package com.example.controller;
import com.example.common.Result;
import com.example.entity.Hotel;
import com.example.service.HotelService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/hotel")
public class HotelController {

    @Resource
    private HotelService hotelService;

    @PostMapping("/add")
    public Result add(@RequestBody Hotel hotel) {
        // 字段校验（示例：名称不能为空，星级1-5）
        if (hotel.getName() == null || hotel.getName().isEmpty()) {
            return Result.error("500","名称不能为空");
        }
        if (hotel.getStarRating() == null || hotel.getStarRating() < 1 || hotel.getStarRating() > 5) {
            return Result.error("500","星级需为1-5");
        }
        
        // 设置默认图片路径，如果未提供
        if (hotel.getImagePath() == null || hotel.getImagePath().isEmpty()) {
            // 根据酒店名称设置默认图片
            String defaultImagePath = getDefaultImagePath(hotel.getName());
            hotel.setImagePath(defaultImagePath);
        }
        
        hotelService.add(hotel);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Hotel hotel) {
        // 字段校验（示例：ID不能为空）
        if (hotel.getId() == null) {
            return Result.error("400","ID不能为空");
        }
        hotelService.update(hotel);
        return Result.success();
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id) {
        hotelService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        hotelService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Long id) {
        Hotel hotel = hotelService.selectById(id);
        return Result.success(hotel);
    }

    @GetMapping("/selectAll")
    public Result selectAll(Hotel hotel) {
        List<Hotel> list = hotelService.selectAll(hotel);
        return Result.success(list);
    }

    @GetMapping("/selectPage")
    public Result selectPage(Hotel hotel,
                           @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Hotel> pageInfo = hotelService.selectPage(hotel, pageNum, pageSize);
        return Result.success(pageInfo);
    }

    @GetMapping("/getMinPriceByHotelId/{hotelId}")
    public Result getMinPriceByHotelId(@PathVariable Long hotelId) {
        Double minPrice = hotelService.getMinPriceByHotelId(hotelId);
        return Result.success(minPrice);
    }
    
    /**
     * 获取默认图片路径
     * 根据系统要求中指定的图片名称规则设置
     */
    private String getDefaultImagePath(String hotelName) {
        if (hotelName.contains("熊猫") || hotelName.contains("长隆")) {
            return "xmjd.jpg";
        } else if (hotelName.contains("外滩") || hotelName.contains("上海")) {
            return "shwthedfjd.jpg";
        } else if (hotelName.contains("金茂") || hotelName.contains("北京")) {
            return "bjjmwl.jpg";
        } else if (hotelName.contains("西湖") || hotelName.contains("杭州")) {
            return "hzxhgbg.jpg";
        }
        // 默认图片
        return "hotel-default.jpg";
    }
}